Skip to content

Metal: define undefined phi values - #1083

Merged
maleadt merged 1 commit into
mainfrom
tb/metal-phi-undef
Nov 7, 2026
Merged

maleadt merged 1 commit into
mainfrom
tb/metal-phi-undef

Conversation

@maleadt

@maleadt maleadt commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

On M3 and newer GPUs (Apple9+), KernelAbstractions' Private testsuite fails: the forloop kernel leaves A[:, 1] unchanged. This reproduces on an M3 Pro (Metal.jl 1.12.0, GPUCompiler 2.21.0, KernelAbstractions 0.10.5) and was also reported on an M6. It passes on the M1 in Metal.jl's CI.

The cause is a phi with an undef incoming value. KernelAbstractions splits a kernel at each @synchronize and wraps every section in a check that skips out-of-range threads. A variable assigned in one section and used in the next, like i = @index(Local, Linear) here, therefore becomes phi [%tid, %valid], [undef, %skipped]. The undef edge is never taken by a thread that uses the value. The AIR handed to Metal still has this shape (checked by disassembling the .air), and it is valid IR. But the AGX back-end then has every thread compute with the same index. Each thread updates the same element per iteration, so after the loop every element has been incremented once instead of N times. Similar kernels throw a spurious BoundsError.

On the reduced kernel, changing only that undef to any defined value (0, or the thread index) fixes the result. The barrier flags, bounds checks, exception lowering and @private all turned out not to matter: the kernel still fails with all of them removed.

This PR replaces undef and poison phi operands with zero at the end of lower_air!. That is a valid refinement. It runs after unify_loop_exits!, which introduces more such phis.

Tested on an M3 Pro:

  • the forloop kernel and 20+ reduced variants give correct results; the spurious BoundsError is gone
  • GPUCompiler metal tests pass (new IR-level test included)
  • Metal.jl's test suite passes against this branch, including the KernelAbstractions testsuite

@codecov

codecov Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.93%. Comparing base (45e224f) to head (cd738f9).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1083      +/-   ##
==========================================
- Coverage   92.86%   91.93%   -0.93%     
==========================================
  Files          31       31              
  Lines        7775     7787      +12     
==========================================
- Hits         7220     7159      -61     
- Misses        555      628      +73     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

The AGX back-end of M3 and newer GPUs miscompiles some phis that have an
undef incoming value, even when that edge is never taken at run time.
KernelAbstractions kernels routinely contain such phis: a variable
assigned before a `@synchronize` and used after it is undefined on the
path that skips the out-of-range threads. KernelAbstractions' `forloop`
test then computed with the same local index in every thread, so all
threads updated the same element, and similar kernels threw spurious
bounds errors.

Replace undef and poison phi operands with zero, after the loop-exit
unification that introduces more of them.
@maleadt
maleadt force-pushed the tb/metal-phi-undef branch from 2a5eb57 to cd738f9 Compare November 7, 2026 16:11
@maleadt
maleadt merged commit 68096a9 into main Nov 7, 2026
32 of 36 checks passed
@maleadt
maleadt deleted the tb/metal-phi-undef branch November 7, 2026 16:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant